package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysCourse;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.CourseStatus;
import com.ruoyi.system.service.ISysCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/system/course")
public class SysCourseController extends BaseController{

    @Autowired
    private ISysCourseService sysCourseService;

    @PreAuthorize("@ss.hasPermi('system:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCourse sysCourse) {
        startPage();
        List<SysCourse> list = sysCourseService.getSysCourseList(sysCourse);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:course:query')")
    @GetMapping("/{courseId}")
    public AjaxResult getInfo(@PathVariable Long courseId) {
        SysCourse sysCourse = sysCourseService.getSysCourseById(courseId);
        return AjaxResult.success(sysCourse);
    }

    @PreAuthorize("@ss.hasPermi('system:course:insert')")
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult insert(@RequestBody SysCourse sysCourse) {
        sysCourse.setCreateBy(getUsername());
        sysCourse.setCourseStatus(CourseStatus.UNACTIVATED.getCode());
        return AjaxResult.success(sysCourseService.insertCourse(sysCourse));
    }

    @PreAuthorize("@ss.hasPermi('system:course:edit')")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody SysCourse sysCourse) {
        sysCourse.setUpdateBy(getUsername());
        return AjaxResult.success(sysCourseService.updateCourse(sysCourse));
    }

    @PreAuthorize("@ss.hasPermi('system:course:delete')")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{curseId}")
    public AjaxResult delete(@PathVariable Long curseId) {
        return AjaxResult.success(sysCourseService.delete(curseId));
    }

    @PreAuthorize("@ss.hasPermi('system:course:activated')")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PostMapping("/activated/{curseId}")
    public AjaxResult activated(@PathVariable Long curseId) {
        return AjaxResult.success(sysCourseService.activated(curseId));
    }
}
